﻿--[[
   @brief    WGSelectZoneItem(区服信息)
   @author   harleyxwang
   @version  1.0
   @date     2017-04-27
--]]

ZoneState2Icon = {
    
    state2icon_ = {};
    state2color_ = {};
    
    init = function()
        ZoneState2Icon.state2icon_[0] = "BtnIconGray";
        ZoneState2Icon.state2icon_[1] = "BtnIconGreen";
        ZoneState2Icon.state2icon_[2] = "BtnIconOrange";
        ZoneState2Icon.state2icon_[3] = "BtnIconRed";

        ZoneState2Icon.state2color_[0] = "RGB:97,97,97";
        ZoneState2Icon.state2color_[1] = "RGB:62,236,0";
        ZoneState2Icon.state2color_[2] = "RGB:255,222,0";
        ZoneState2Icon.state2color_[3] = "RGB:255,0,0";
    end;
    
    get_icon_name = function (state)
        if ZoneState2Icon.state2icon_[state] ~= nil then
            return ZoneState2Icon.state2icon_[state];
        else
            return "BtnIconInvalid";
        end
    end;

    get_state_color = function (state)
        if ZoneState2Icon.state2color_[state] ~= nil then
            return ZoneState2Icon.state2color_[state];
        else
            return "Invalid";
        end
    end;
};

 WGSelectZoneItem = {
     DELAY_GREEN_THRESHOLD = 100;
     DELAY_YELLOW_THRESHOLD = 300;
     DELAY_GREEN = 1;
     DELAY_YELLOW = 2;
     DELAY_RED = 3;
};

function WGSelectZoneItem:log(msg, core_msg)
    base_utility.log("[WGSelect][Zone]" .. msg, core_msg);
end

function WGSelectZoneItem:set_server_info(item_or_edit_id, server_info)
    local uob_content_id = ui_utility.get_ctrl_id_by_path(item_or_edit_id, "UobContent");
    local delay_txt_id = ui_utility.get_ctrl_id_by_path(
        item_or_edit_id, "UobContent\\CntItemContent\\delayTime");
    if delay_txt_id ~= Tenio.INVALID_CONTROLID then
        self:set_delay(delay_txt_id, server_info.acce_info);
    end;
    
    self:set_zone_position(uob_content_id, server_info);
    return self:set_zone_info(uob_content_id, server_info);
end

function WGSelectZoneItem:set_zone_position(uob_content_id, server_info)
    local zoneCtrl = ui_utility.get_ctrl_ptr_by_path(uob_content_id, "CntItemContent\\BtnZoneName");
    local zoneRect = zoneCtrl:GetRectEx(FALSE,TRUE);
    local delayCtrl = ui_utility.get_ctrl_ptr_by_path(uob_content_id, "CntItemContent\\delayTime");
    local delayRect = delayCtrl:GetRectEx(FALSE,TRUE);
    local delayWidth = delayRect.right - delayRect.left;
    local uobCtrl = ui_utility.get_ctrl_ptr_by_id(uob_content_id);
    local uobRect = uobCtrl:GetRectEx(FALSE,TRUE);
    local uobWidth = uobRect.right - uobRect.left;
    if server_info.acce_info.valid == 1  then
        delayRect.right = uobWidth - 25;
        delayRect.left = delayRect.right - delayWidth;
        delayCtrl:MoveEx(delayRect,TRUE);
        zoneRect.right = delayRect.left - 2;
        zoneCtrl:MoveEx(zoneRect,TRUE);
    else
        zoneRect.right = uobWidth;
        zoneCtrl:MoveEx(zoneRect,TRUE);
    end;
    MainCommon.set_button_caption(ui_utility.uilib(), uob_content_id, "CntItemContent\\BtnZoneName",
        server_info.name);   
end;

function WGSelectZoneItem:set_zone_info(uob_content_id, server_info)
    if server_info == nil then
        self:log(string.format("set_zone_info, server_info == nil, uob_content_id:%d",
            uob_content_id), true);
        return false;
    end
    self:log(string.format("set_zone_info, uob_content_id:%d, zone id:%d", uob_content_id, server_info.id));
    self:show_server_state(uob_content_id, server_info.state);
    return MainCommon.set_button_caption(ui_utility.uilib(),uob_content_id, "CntItemContent\\BtnZoneName",
       server_info.name);
end

function WGSelectZoneItem:show_server_state(uob_content_id, state)
    self:log("show_server_state, state:" .. tostring(state));
    local icon_name = ZoneState2Icon.get_icon_name(state);
    local icons_id = MainCommon.get_child_id(ui_utility.uilib(), uob_content_id,
                                  "CntItemContent\\CntIcons");
    -- 刷新图标及状态
    MainCommon.show_only_child(ui_utility.uilib(), icons_id, icon_name);
end

function WGSelectZoneItem:set_item_or_edit(item_or_edit_id, game_id, server_info)
    self:log("set_item_or_edit");
    local uob_content_id = ui_utility.get_ctrl_id_by_path(item_or_edit_id, "UobContent");
    local delay_txt_id = ui_utility.get_ctrl_id_by_path(
        item_or_edit_id, "UobContent\\CntItemContent\\delayTime");
    if delay_txt_id ~= Tenio.INVALID_CONTROLID then
        self:set_delay(delay_txt_id, game_id, server_info.id, server_info.acce_info);
    end;
    
    -- self:set_zone_position(uob_content_id, server_info);
    return self:set_zone_info(uob_content_id, server_info);
end

function WGSelectZoneItem:set_delay(delay_txt_id, game_id, zone_id, acce_info)
    local delay_txt_ptr = ui_utility.get_ctrl_ptr_by_id(delay_txt_id);
    if delay_txt_ptr == nil then
        self:log(string.format("set_delay, cant find delayTime Ctrl ptr"));
        return;
    end;

    if acce_info.valid ~= 1 then
        delay_txt_ptr:Show(FALSE);
        return;
    end;

    -- jameszhai: 天刀不显示PING值
    local   zParam = {};

    zParam["game_id"] = game_id;

    local zTemp = comm_center.call_service("Svr_IsXGame", zParam);

    if (nil ~= zTemp["is_xgame"]) and (0 ~= zTemp["is_xgame"]) then
        delay_txt_ptr:Show(FALSE);
    else
        delay_txt_ptr:Show(TRUE);
    end;

    local use_proxy, display_delay, display_delay_txt =
        self:get_acce_status(game_id, zone_id, acce_info);

    ui_utility.setproperty_by_id(delay_txt_id, 'Caption', display_delay_txt);
    local delay_state = WGSelectZoneItem.DELAY_GREEN;
    if display_delay < WGSelectZoneItem.DELAY_GREEN_THRESHOLD then
        delay_state = WGSelectZoneItem.DELAY_GREEN;
    elseif  display_delay <  WGSelectZoneItem.DELAY_YELLOW_THRESHOLD then
        delay_state = WGSelectZoneItem.DELAY_YELLOW;
    else
        delay_state = WGSelectZoneItem.DELAY_RED;
    end

    local state_color = ZoneState2Icon.get_state_color(delay_state);
    ui_utility.setproperty_by_id(delay_txt_id, "TextColor", state_color);
end;

function WGSelectZoneItem:set_delay_tip(delay_txt_id, acce_info)
    --[[
    1. 有加速：
        1.1 智能加速（肯定加速优）：加速成功，已选择最佳路由，延迟：xms 提升：xx%
        1.2 自定义加速：
            1.2.1 加速优：加速成功，已选择最佳路由，延迟：xms 提升：xx%
            1.2.2 直连优：建议切换为智能加速，延迟：xms 下降：xx%
    2. 无加速：
        2.1 智能加速：暂无更优节点推荐，未加速 如需加速可尝试自定义加速
        2.2 自定义加速（不会发生）：暂无更优节点推荐，未加速
    3. 关闭加速：加速引擎已关闭 点击查看详情
    ]]
    local tip_text = '';
    local use_proxy, display_delay, display_delay_txt = self:get_acce_status(acce_info);
    if use_proxy == true then
        if acce_info.valid == 2 then -- 这个分支理论上永远进不来
            tip_text = '<f c="0xfefefe">' .. LoadStr("FRAME_BOTTOM_ACCELERATE_ZONELESS") .. '</f>';
        elseif display_delay > 100 and self:is_multi_isps() then
            tip_text = '<f c="0xfefefe">' .. LoadStr("FRAME_BOTTOM_NETWORK_EXIST_MORE") .. '</f>';
        elseif acce_info.delay_down > 0 then
            tip_text = '<f c="0xfefefe">' .. LoadStr("FRAME_BOTTOM_NETWORK_ACCELERATE_SUCCESS") .. '</f><br><f c="0x545454">' .. LoadStr("FRAME_BOTTOM_NETWORK_DELAY") .. '</f>' .. 
            '<f c="0x00ff00">' .. display_delay_txt .. '</f><f c="0x545454"> ' .. LoadStr("FRAME_BOTTOM_NETWORK_IMPROVE") .. '</f>' .. 
            '<f c="0x00ff00">' .. acce_info.delay_down .. '%</f>';
        else
            local display_down_txt = (-acce_info.delay_down) .. '';
            if acce_info.delay_down < -100 then
                display_down_txt = '100+';
            end;
            tip_text = '<f c="0xfefefe">' .. LoadStr("FRAME_BOTTOM_NETWORK_ADIVCE_SWITCH_INTELLIGENT") .. '</f><br><f c="0x545454">' .. LoadStr("FRAME_BOTTOM_NETWORK_DELAY") .. '</f>' .. 
            '<f c="0x2255CD">' .. display_delay_txt .. '</f><f c="0x545454"> ' .. LoadStr("FRAME_BOTTOM_NETWORK_DESCEND") .. '</f>' .. 
            '<f c="0x2255CD">' .. display_down_txt .. '%</f>';
        end;
    else
        if acce_info.acce_mode == 0 then
            tip_text = '<f c="0xfefefe">' .. LoadStr("FRAME_BOTTOM_NETWORK_ENGINE_CLOSED") .. '</f><br><f c="0xfefefe">' .. LoadStr("FRAME_BOTTOM_NETWORK_CLICK_FOR_DETAIL") .. '</f>';
        elseif acce_info.acce_mode == 1 then
            tip_text = '<f c="0xfefefe">' .. LoadStr("FRAME_BOTTOM_NETWORK_NO_RECOMMEND") .. '</f><br><f c="0xfefefe">' .. LoadStr("FRAME_BOTTOM_NETWORK_SELFDEFINE_ACCELERATE") .. '</f>';
        else
            tip_text = '<f c="0xfefefe">' .. LoadStr("FRAME_BOTTOM_NETWORK_NO_RECOMMEND") .. '</f>';
        end;
    end;
    ui_utility.setproperty_by_id(delay_txt_id, "TipName", "AcceToolTip");  
    ui_utility.setproperty_by_id(delay_txt_id, "Tip", tip_text); 
end;

function WGSelectZoneItem:refresh_acce_info(acce_info)
    local cnt_zone = self:GetCntZone();
    if not cnt_zone then
        return;
    end
    
    local CntZone_id = cnt_zone:GetControlID();
    local delay_txt_id = ui_utility.get_ctrl_id_by_path(
        CntZone_id, "UobContent\\CntItemContent\\delayTime");
    if delay_txt_id == Tenio.INVALID_CONTROLID then
        self:log(string.format("refresh_acce_info, cant find delayTime Ctrl id"));
        return;
    end;

    self:set_delay(delay_txt_id, acce_info);
    self:set_delay_tip(delay_txt_id, acce_info);
end

function WGSelectZoneItem:get_acce_status(game_id, zone_id, acce_info)
    local use_proxy = false;
    if acce_info.decision_code == 0 or acce_info.decision_code == 4 or
        acce_info.decision_code == 8 then
        use_proxy = true;
    end;

    local display_delay = acce_info.proxy_delay_raw_t1 + acce_info.proxy_delay_raw_t2;
    if use_proxy == false then
        display_delay = acce_info.direct_delay_raw;
    end;

    local acce_module =
        base_utility.get_component_adapter("IAccelerate_module",
            "ierd_tgp.accelerate_module.GetAcceModule();");
    if acce_module ~= nil then
        local real_ping = acce_module:get_real_ping(game_id, zone_id);
        if real_ping ~= -1 then
            display_delay = real_ping;
        end;
    end;

    local display_delay_txt = display_delay .. 'ms';
    -- 此种情况是代表无加速链路，根据产品要求，此处改为默认显示40
    if display_delay > 999 then
        display_delay_txt = '>40ms';
    end;

    return use_proxy, display_delay, display_delay_txt;
end;

function WGSelectZoneItem:is_multi_isps()
    local acceModule =
        base_utility.get_component_adapter("IAccelerate_module",
            "ierd_tgp.accelerate_module.GetAcceModule();");
    local info = ierd_tgp.accelerate_module.AcceCenterBaseInfoPOD:new();
    acceModule:get_acc_center_basic_info(info);
    local ret = (info.dwExport_ip_cnt > 0);
    info:delete();
    return ret;
end

ZoneState2Icon.init();